package com.atguigu.lease.web.app.service.impl;

import com.atguigu.lease.model.entity.*;
import com.atguigu.lease.model.enums.ItemType;
import com.atguigu.lease.model.enums.LeaseStatus;
import com.atguigu.lease.web.app.mapper.*;
import com.atguigu.lease.web.app.service.*;
import com.atguigu.lease.web.app.vo.apartment.ApartmentItemVo;
import com.atguigu.lease.web.app.vo.attr.AttrValueVo;
import com.atguigu.lease.web.app.vo.fee.FeeValueVo;
import com.atguigu.lease.web.app.vo.graph.GraphVo;
import com.atguigu.lease.web.app.vo.room.RoomDetailVo;
import com.atguigu.lease.web.app.vo.room.RoomItemVo;
import com.atguigu.lease.web.app.vo.room.RoomQueryVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author liubo
 * @description 针对表【room_info(房间信息表)】的数据库操作Service实现
 * @createDate 2023-07-26 11:12:39
 */
@Service
@Slf4j
public class RoomInfoServiceImpl extends ServiceImpl<RoomInfoMapper, RoomInfo>
        implements RoomInfoService {
    @Autowired
    private RoomInfoMapper roomInfoMapper;
    @Autowired
    private GraphInfoService graphInfoService;
    @Autowired
    private ApartmentInfoMapper apartmentInfoMapper;
    @Autowired
    private AttrValueMapper attrValueMapper;
    @Autowired
    private FacilityInfoMapper facilityInfoMapper;
    @Autowired
    private LabelInfoMapper labelInfoMapper;
    @Autowired
    private PaymentTypeMapper paymentTypeMapper;
    @Autowired
    private FeeValueMapper feeValueMapper;
    @Autowired
    private LeaseTermMapper leaseTermMapper;
    @Autowired
    private LeaseAgreementMapper leaseAgreementMapper ;
    /**
     * 分页查询房间列表
     * 多表联查
     * 设计表：房间、公寓、图片、标签、房间-标签
     * @param page
     * @param queryVo
     */
    @Override
    public void customPageItem(Page<RoomItemVo> page, RoomQueryVo queryVo) {
        roomInfoMapper.customSelectList(page,queryVo);

    }

    /**
     * 根据公寓id分页查询房间列表
     * 表：房间、公寓、图片、标签、房间-标签
     * @param id
     * @return
     */
    @Override
    public void customPageItemByApartmentId(Page<RoomItemVo> page, Long id) {
        roomInfoMapper.customSelectListByApartmentId(page,id);
    }

    /**
     * 根据id获取房间的详细信息
     *      根据房间id查房间
     *      根据房间id，查图片vo,单表
     *      根据公寓id查公寓详情，公寓，多表连接，自定义mapper
     *      根据房间id,查属性值vo,多表：房间-属性值，属性值，属性名
     *      根据房间id，查询配套，
     *      根据房间id,查询标签
     *      根据房间id，查询支付方式
     *      根据房间id，查询杂费值VO
     * @param id
     * @return
     */
    @Override
    public RoomDetailVo customGetDetailById(Long id) {
        //根据房间id查房间
        RoomInfo roomInfo = roomInfoMapper.selectById(id);
        //根据房间id，查图片vo,单表
        List<GraphVo> graphVoList = graphInfoService.customList(ItemType.ROOM,id);
        //根据公寓id查公寓详情，公寓，多表连接，自定义mapper
        ApartmentItemVo apartmentItemVo=apartmentInfoMapper.customSelectListById(roomInfo.getApartmentId());
        //根据房间id,查属性值vo,多表：房间-属性值，属性值，属性名
        List<AttrValueVo> attrValueVoList=attrValueMapper.customSelectListByRoomId(id);
        //根据房间id，查询配套，
        List<FacilityInfo> facilityInfoList=facilityInfoMapper.customSelectListById(id);
        //根据房间id,查询标签
        List<LabelInfo> labelInfoList=labelInfoMapper.customSelectListByRoomId(id);
        //根据房间id，查询支付方式
        List<PaymentType> paymentTypeList=paymentTypeMapper.customSelectList(id);
        //根据房间id，查询杂费值VO
        List<FeeValueVo> feeValueVoList=feeValueMapper.customSelectListById(roomInfo.getApartmentId());
        //根据房间id，查询租期
        List<LeaseTerm> leaseTermList=leaseTermMapper.customSelectListByRoomId(id);
        //查询是否入住,count:in 2 5 7
        LambdaQueryWrapper<LeaseAgreement> leaseAgreementLambdaQueryWrapper=new LambdaQueryWrapper();
        leaseAgreementLambdaQueryWrapper.eq(LeaseAgreement::getRoomId,id);
        leaseAgreementLambdaQueryWrapper.in(LeaseAgreement::getStatus,
                                            LeaseStatus.SIGNED,
                                            LeaseStatus.WITHDRAWING,
                                            LeaseStatus.RENEWING);
        Long count=leaseAgreementMapper.selectCount(leaseAgreementLambdaQueryWrapper);
        //封装进roomDetailVo，
        RoomDetailVo roomDetailVo = new RoomDetailVo();
        BeanUtils.copyProperties(roomInfo,roomDetailVo);
        roomDetailVo.setIsDelete(roomInfo.getIsDeleted()==1);
        roomDetailVo.setIsCheckIn(count>0);
        roomDetailVo.setApartmentItemVo(apartmentItemVo);
        roomDetailVo.setGraphVoList(graphVoList);
        roomDetailVo.setAttrValueVoList(attrValueVoList);
        roomDetailVo.setFacilityInfoList(facilityInfoList);
        roomDetailVo.setLabelInfoList(labelInfoList);
        roomDetailVo.setPaymentTypeList(paymentTypeList);
        roomDetailVo.setFeeValueVoList(feeValueVoList);
        roomDetailVo.setLeaseTermList(leaseTermList);
        //返回
        return roomDetailVo;
    }

}




